package com.yzt.message.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.message.R;
import com.yzt.redpack.adapter.GameTransHostAdapter;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.mvp.CommonPresenter;
import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GameUserListBean;
import com.yzt.zhuju.network.entity.message.GroupBaseInfo;
import com.yzt.zhuju.network.entity.message.GroupUserInfo;
import com.yzt.zhuju.network.entity.mine.FriendBaseBean;
import com.yzt.zhuju.network.entity.mine.FriendUserBean;
import com.yzt.zhuju.widget.tuiview.TUIEditText;
import com.yzt.zhuju.widget.tuiview.TUITextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 00:14:20
 * 描述：创建群聊
 */
public class GroupCreateActivity extends MVPActivity<CommonPresenter> {

    private GameTransHostAdapter adapter;

    TUITextView tv_create;

    private int type;

    private String groupId;

    public static void startSelf(Context activity, int type, String groupId) {
        activity.startActivity(new Intent(activity, GroupCreateActivity.class)
                .putExtra("extra_type", type)
                .putExtra("extra_groupId", groupId));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_create_group;
    }

    @Override
    public void installViews() {

        type = getIntent().getIntExtra("extra_type", 0);
        groupId = getIntent().getStringExtra("extra_groupId");

        LinearLayout ll_search = findViewById(R.id.ll_search);
        TUIEditText et_content = findViewById(R.id.et_content);
        TextView tv_search = findViewById(R.id.tv_search);
        tv_create = findViewById(R.id.tv_create);

        if (type == 0) {
            mTitleBar.setTitle("创建群聊");
            ll_search.setVisibility(View.VISIBLE);
            tv_create.setText("发起群聊");
        } else if (type == -1) {
            mTitleBar.setTitle("邀请好友");
            tv_create.setText("邀请好友");
        } else {
            mTitleBar.setTitle("移除成员");
            tv_create.setText("移除成员");
        }

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new GameTransHostAdapter();
        recycle.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view, position) -> {
            GameUserListBean bean = adapter.getItem(position);
            bean.setChecked(!bean.isChecked());
            adapter.notifyItemChanged(position);
            int i = 0;
            for (GameUserListBean data : adapter.getData()) {
                if (data.isChecked()) {
                    i++;
                }
            }

            if (type == 0) {
                tv_create.setText("发起群聊(" + i + ")");
            } else if (type == -1) {
                tv_create.setText("邀请好友(" + i + ")");
            } else {
                tv_create.setText("移除成员(" + i + ")");
            }
        });

        tv_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> list = new ArrayList<>();
                for (GameUserListBean data : adapter.getData()) {
                    if (data.isChecked()) {
                        list.add(data.getUserId());
                    }
                }

                if (list.size() == 0) {
                    showToast("请选择用户对象");
                    return;
                }

                if (type == -2) {
                    BizLayer.getInstance().getMessageModule().groupKick(groupId, list, new AresSubscriber<Object>() {
                        @Override
                        protected void onSuccess(Object bean) {
                            showToast("移除成功！");
                            finish();
                        }

                        @Override
                        protected void onError(int code, String info) {
                            super.onError(code, info);
                        }

                        @Override
                        protected void onFinallyEnd() {
                            super.onFinallyEnd();
                        }
                    });
                } else if (type == -1) {
                    BizLayer.getInstance().getMessageModule().groupInvite(groupId, list, new AresSubscriber<Object>() {
                        @Override
                        protected void onSuccess(Object bean) {
                            showToast("邀请成功！");
                            finish();
                        }

                        @Override
                        protected void onError(int code, String info) {
                            super.onError(code, info);
                        }

                        @Override
                        protected void onFinallyEnd() {
                            super.onFinallyEnd();
                        }
                    });
                } else {
                    GroupCreateStep2Activity.start(GroupCreateActivity.this, list, 10000);
                }
            }
        });

        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String etContent = et_content.getText().toString();
                if (etContent.equals("")) {
                    showToast("请输入搜索内容");
                    return;
                }
                BizLayer.getInstance().getAccountModule().friendList(etContent, RXingUtils.getUserId(), 1, 10000, new AresSubscriber<FriendBaseBean>() {
                    @Override
                    protected void onSuccess(FriendBaseBean bean) {
                        List<FriendUserBean> data = bean.getList();
                        List<GameUserListBean> list = new ArrayList<>();
                        for (FriendUserBean user : data) {
                            GameUserListBean userBean = new GameUserListBean();
                            userBean.setAvatar(user.getAvatar());
                            userBean.setGender(user.getGender());
                            userBean.setNickName(user.getNickName());
                            userBean.setUserId(user.getUserId());
                            list.add(userBean);
                        }
                        adapter.setList(list);
                    }

                    @Override
                    protected void onError(int code, String info) {
                        super.onError(code, info);
                    }

                    @Override
                    protected void onFinallyEnd() {
                        super.onFinallyEnd();
                    }
                });
            }
        });

        getHttpData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && resultCode == RESULT_OK) {
            finish();
        }
    }

    /**
     * 获取数据
     */
    public void getHttpData() {
        if (type == -2) {
            // 删除对象 - 获取群成员列表
            BizLayer.getInstance().getMessageModule().groupChatUserList(groupId, new AresSubscriber<GroupBaseInfo>() {
                @Override
                protected void onSuccess(GroupBaseInfo bean) {
                    List<GroupUserInfo> data = bean.getMemberList();
                    List<GameUserListBean> list = new ArrayList<>();
                    for (GroupUserInfo user : data) {
                        GameUserListBean userBean = new GameUserListBean();
                        userBean.setAvatar(user.getAvatar());
                        userBean.setGender(user.getGender());
                        userBean.setNickName(user.getMemberAlias());
                        userBean.setUserId(user.getUserId());
                        list.add(userBean);
                    }
                    adapter.setList(list);
                }

                @Override
                protected void onError(int code, String info) {
                    super.onError(code, info);
                }

                @Override
                protected void onFinallyEnd() {
                    super.onFinallyEnd();
                }
            });
        } else {
            // 添加对象 - 获取好友列表 这里需要移除群已存在的群成员
            BizLayer.getInstance().getAccountModule().friendList("", RXingUtils.getUserId(), 1, 10000, new AresSubscriber<FriendBaseBean>() {
                @Override
                protected void onSuccess(FriendBaseBean bean) {
                    List<FriendUserBean> data = bean.getList();
                    List<GameUserListBean> list = new ArrayList<>();
                    for (FriendUserBean user : data) {
                        GameUserListBean userBean = new GameUserListBean();
                        userBean.setAvatar(user.getAvatar());
                        userBean.setGender(user.getGender());
                        userBean.setNickName(user.getNickName());
                        userBean.setUserId(user.getUserId());
                        list.add(userBean);
                    }
                    adapter.setList(list);
                }

                @Override
                protected void onError(int code, String info) {
                    super.onError(code, info);
                }

                @Override
                protected void onFinallyEnd() {
                    super.onFinallyEnd();
                }
            });
        }
    }
}